package io.renren.modules.pay.controller;

import io.renren.common.utils.Result;
import io.renren.modules.pay.service.GoogleauthService;
import io.renren.modules.security.user.SecurityUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;



/**
 * 谷歌验证
 *
 * @author ms 
 * @since 1.0.0 2019-06-24
 */
@RestController
@RequestMapping("pay/googleauth")
@Api(tags="谷歌验证")
public class GoogleauthController {
    @Autowired
    private GoogleauthService googleauthService;
    
	/**
	 * 生成包含用户信息,密钥的二维码图片
	 */
	@GetMapping("googleAuthQrCode")
	@ApiOperation("二维码")
	@ResponseBody
	@CrossOrigin
	public Result<Object> googleAuthQrCode(@RequestParam String service) {
		Result<Object> reslut =	googleauthService.createGoogleAuthQRCodeData(service);
		return reslut;
	}
	
	/**
	 * 绑定时 Google Authenticator 的校验
	 */

	@PostMapping("bindVerify")
	public Result<Object>  bindVerify(@RequestBody Map<String, Object> params) {
		//根据用户信息从数据库中获取保存时的密钥
		params.put("userid", SecurityUser.getUser().getId());
		 Result<Object> reslut =googleauthService.verify(params);
		return reslut;
	}



	/**
	 * 开启 Google Authenticator 服务的用户备用登录
	 */
	@RequestMapping("/loginBackupVerify")
	@ResponseBody
	public Map<String, Object> loginBackupVerify(String code) {
		Map<String, Object> result = new HashMap<>();
		//根据用户id获取备用验证码信息
		String backupCodes = "bcasjgg4vm44,qc2ifk6sjmig,744cbq5cfmrx";
		boolean success = false;
		for (String backupCode : backupCodes.split(",")) {
			if (backupCode.equals(code)) {
				success = true;
				//同时为了安全性,备用验证码在验证成功后应该从数据库中删除
			}
		}
		result.put("success", success);
		return result;

	}


 
 
}